<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>boost/python/iterator.hpp</title>
<link rel="stylesheet" href="../../boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../index.html" title="Boost.Python Reference Manual">
<link rel="up" href="../high_level_components.html" title="Chapter 2. High Level Components">
<link rel="prev" href="boost_python_init_hpp.html" title="boost/python/init.hpp">
<link rel="next" href="boost_python_module_hpp.html" title="boost/python/module.hpp">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr><td valign="top"><img alt="" width="" height="" src="../../images/boost.png"></td></tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="boost_python_init_hpp.html"><img src="../../images/prev.png" alt="Prev"></a><a accesskey="u" href="../high_level_components.html"><img src="../../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="boost_python_module_hpp.html"><img src="../../images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="high_level_components.boost_python_iterator_hpp"></a><a class="link" href="boost_python_iterator_hpp.html" title="boost/python/iterator.hpp">boost/python/iterator.hpp</a>
</h2></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="boost_python_iterator_hpp.html#high_level_components.boost_python_iterator_hpp.introduction">Introduction</a></span></dt>
<dt><span class="section"><a href="boost_python_iterator_hpp.html#high_level_components.boost_python_iterator_hpp.class_template_iterator">Class
        template <code class="computeroutput"><span class="identifier">iterator</span></code></a></span></dt>
<dt><span class="section"><a href="boost_python_iterator_hpp.html#high_level_components.boost_python_iterator_hpp.class_template_iterator_construc">Class
        template iterator constructors</a></span></dt>
<dt><span class="section"><a href="boost_python_iterator_hpp.html#high_level_components.boost_python_iterator_hpp.class_template_iterators">Class
        template <code class="computeroutput"><span class="identifier">iterators</span></code></a></span></dt>
<dt><span class="section"><a href="boost_python_iterator_hpp.html#high_level_components.boost_python_iterator_hpp.class_template_iterators_nested_">Class
        template iterators nested types</a></span></dt>
<dt><span class="section"><a href="boost_python_iterator_hpp.html#high_level_components.boost_python_iterator_hpp.class_template_iterators_static_">Class
        template iterators static functions</a></span></dt>
<dt><span class="section"><a href="boost_python_iterator_hpp.html#high_level_components.boost_python_iterator_hpp.functions">Functions</a></span></dt>
<dt><span class="section"><a href="boost_python_iterator_hpp.html#high_level_components.boost_python_iterator_hpp.example">Example</a></span></dt>
</dl></div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="high_level_components.boost_python_iterator_hpp.introduction"></a><a class="link" href="boost_python_iterator_hpp.html#high_level_components.boost_python_iterator_hpp.introduction" title="Introduction">Introduction</a>
</h3></div></div></div>
<p>
          &lt;boost/python/iterator.hpp&gt; provides types and functions for creating
          <a href="http://www.python.org/doc/current/lib/typeiter.html" target="_top">Python
          iterators</a> from C++ Containers and Iterators. Note that if your
          <code class="computeroutput"><span class="identifier">class_</span></code> supports random-access
          iterators, implementing <a href="http://www.python.org/doc/current/ref/sequence-types.html#l2h-128" target="_top">__getitem__</a>
          (also known as the Sequence Protocol) may serve you better than using this
          facility: Python will automatically create an iterator type for you (see
          <a href="http://www.python.org/doc/current/lib/built-in-funcs.html#l2h-35" target="_top"><code class="computeroutput"><span class="identifier">iter</span><span class="special">()</span></code></a>),
          and each access can be range-checked, leaving no possiblity of accessing
          through an invalidated C++ iterator.
        </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="high_level_components.boost_python_iterator_hpp.class_template_iterator"></a><a class="link" href="boost_python_iterator_hpp.html#high_level_components.boost_python_iterator_hpp.class_template_iterator" title="Class template iterator">Class
        template <code class="computeroutput"><span class="identifier">iterator</span></code></a>
</h3></div></div></div>
<p>
          Instances of <code class="computeroutput"><span class="identifier">iterator</span><span class="special">&lt;</span><span class="identifier">C</span><span class="special">,</span><span class="identifier">P</span><span class="special">&gt;</span></code> hold a reference to a callable Python
          object which, when invoked from Python, expects a single argument c convertible
          to C and creates a Python iterator that traverses <code class="computeroutput"><span class="special">[</span><span class="identifier">c</span><span class="special">.</span><span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">c</span><span class="special">.</span><span class="identifier">end</span><span class="special">())</span></code>. The optional <a class="link" href="../concepts.html#concepts.callpolicies" title="CallPolicies">CallPolicies</a>
          <code class="computeroutput"><span class="identifier">P</span></code> can be used to control
          how elements are returned during iteration.
        </p>
<p>
          In the table below, c is an instance of Container.
        </p>
<div class="informaltable"><table class="table">
<colgroup>
<col>
<col>
<col>
<col>
</colgroup>
<thead><tr>
<th>
                  <p>
                    Template Parameter
                  </p>
                </th>
<th>
                  <p>
                    Requirements
                  </p>
                </th>
<th>
                  <p>
                    Semantics
                  </p>
                </th>
<th>
                  <p>
                    Default
                  </p>
                </th>
</tr></thead>
<tbody>
<tr>
<td>
                  <p>
                    Container
                  </p>
                </td>
<td>
                  <p>
                    <code class="computeroutput"><span class="special">[</span><span class="identifier">c</span><span class="special">.</span><span class="identifier">begin</span><span class="special">(),</span><span class="identifier">c</span><span class="special">.</span><span class="identifier">end</span><span class="special">()</span></code>) is a valid Iterator range.
                  </p>
                </td>
<td>
                  <p>
                    The result will convert its argument to c and call c.begin()
                    and c.end() to acquire iterators. To invoke Container's const
                    <code class="computeroutput"><span class="identifier">begin</span><span class="special">()</span></code>
                    and <code class="computeroutput"><span class="identifier">end</span><span class="special">()</span></code>
                    functions, make it const.
                  </p>
                </td>
<td>
                </td>
</tr>
<tr>
<td>
                  <p>
                    NextPolicies
                  </p>
                </td>
<td>
                  <p>
                    A default-constructible model of <a class="link" href="../concepts.html#concepts.callpolicies" title="CallPolicies">CallPolicies</a>.
                  </p>
                </td>
<td>
                  <p>
                    Applied to the resulting iterators' <code class="computeroutput"><span class="identifier">next</span><span class="special">()</span></code> method.
                  </p>
                </td>
<td>
                  <p>
                    An unspecified model of <a class="link" href="../concepts.html#concepts.callpolicies" title="CallPolicies">CallPolicies</a>
                    which always makes a copy of the result of deferencing the underlying
                    C++ iterator
                  </p>
                </td>
</tr>
</tbody>
</table></div>
<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">python</span>
  <span class="special">{</span>
    <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">Container</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">NextPolicies</span> <span class="special">=</span> <span class="identifier">unspecified</span><span class="special">&gt;</span>
    <span class="keyword">struct</span> <span class="identifier">iterator</span> <span class="special">:</span> <span class="identifier">object</span>
    <span class="special">{</span>
      <span class="identifier">iterator</span><span class="special">();</span>
    <span class="special">};</span>
  <span class="special">}}</span>
</pre>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="high_level_components.boost_python_iterator_hpp.class_template_iterator_construc"></a><a class="link" href="boost_python_iterator_hpp.html#high_level_components.boost_python_iterator_hpp.class_template_iterator_construc" title="Class template iterator constructors">Class
        template iterator constructors</a>
</h3></div></div></div>
<pre class="programlisting"><span class="identifier">iterator</span><span class="special">()</span></pre>
<div class="variablelist">
<p class="title"><b></b></p>
<dl class="variablelist">
<dt><span class="term">Effects</span></dt>
<dd>
<p>
                Initializes its base class with the result of:
              </p>
<pre class="programlisting"><span class="identifier">range</span><span class="special">&lt;</span><span class="identifier">NextPolicies</span><span class="special">&gt;(&amp;</span><span class="identifier">iterators</span><span class="special">&lt;</span><span class="identifier">Container</span><span class="special">&gt;::</span><span class="identifier">begin</span><span class="special">,</span> <span class="special">&amp;</span><span class="identifier">iterators</span><span class="special">&lt;</span><span class="identifier">Container</span><span class="special">&gt;::</span><span class="identifier">end</span><span class="special">)</span></pre>
</dd>
<dt><span class="term">Postconditions</span></dt>
<dd><p>
                <code class="computeroutput"><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">get</span><span class="special">()</span></code>
                points to a Python callable object which creates a Python iterator
                as described above.
              </p></dd>
<dt><span class="term">Rationale</span></dt>
<dd><p>
                Provides an easy way to create iterators for the common case where
                a C++ class being wrapped provides <code class="computeroutput"><span class="identifier">begin</span><span class="special">()</span></code> and <code class="computeroutput"><span class="identifier">end</span><span class="special">()</span></code>.
              </p></dd>
</dl>
</div>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="high_level_components.boost_python_iterator_hpp.class_template_iterators"></a><a class="link" href="boost_python_iterator_hpp.html#high_level_components.boost_python_iterator_hpp.class_template_iterators" title="Class template iterators">Class
        template <code class="computeroutput"><span class="identifier">iterators</span></code></a>
</h3></div></div></div>
<p>
          A utility class template which provides a way to reliably call its argument's
          <code class="computeroutput"><span class="identifier">begin</span><span class="special">()</span></code>
          and <code class="computeroutput"><span class="identifier">end</span><span class="special">()</span></code>
          member functions. Note that there is no portable way to take the address
          of a member function of a C++ standard library container, so <code class="computeroutput"><span class="identifier">iterators</span><span class="special">&lt;&gt;</span></code>
          can be particularly helpful when wrapping them.
        </p>
<p>
          In the table below, x is an instance of C.
        </p>
<div class="informaltable"><table class="table">
<colgroup>
<col>
<col>
</colgroup>
<thead><tr>
<th>
                  <p>
                    Required Valid Expression
                  </p>
                </th>
<th>
                  <p>
                    Type
                  </p>
                </th>
</tr></thead>
<tbody>
<tr>
<td>
                  <p>
                    x.begin()
                  </p>
                </td>
<td>
                  <p>
                    Convertible to C::const_iterator if C is a const type; convertible
                    to C::iterator otherwise.
                  </p>
                </td>
</tr>
<tr>
<td>
                  <p>
                    x.end()
                  </p>
                </td>
<td>
                  <p>
                    Convertible to C::const_iterator if C is a const type; convertible
                    to C::iterator otherwise.
                  </p>
                </td>
</tr>
</tbody>
</table></div>
<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">python</span>
<span class="special">{</span>
  <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">C</span><span class="special">&gt;</span>
  <span class="keyword">struct</span> <span class="identifier">iterators</span>
  <span class="special">{</span>
      <span class="keyword">typedef</span> <span class="keyword">typename</span> <span class="identifier">C</span><span class="special">::</span><span class="identifier">const_iterator</span> <span class="identifier">iterator</span><span class="special">;</span>
      <span class="keyword">static</span> <span class="identifier">iterator</span> <span class="identifier">begin</span><span class="special">(</span><span class="identifier">C</span><span class="special">&amp;</span> <span class="identifier">x</span><span class="special">);</span>
      <span class="keyword">static</span> <span class="identifier">iterator</span> <span class="identifier">end</span><span class="special">(</span><span class="identifier">C</span><span class="special">&amp;</span> <span class="identifier">x</span><span class="special">);</span>
  <span class="special">};</span>
<span class="special">}}</span>
</pre>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="high_level_components.boost_python_iterator_hpp.class_template_iterators_nested_"></a><a class="link" href="boost_python_iterator_hpp.html#high_level_components.boost_python_iterator_hpp.class_template_iterators_nested_" title="Class template iterators nested types">Class
        template iterators nested types</a>
</h3></div></div></div>
<p>
          If C is a const type,
        </p>
<pre class="programlisting"><span class="keyword">typedef</span> <span class="keyword">typename</span> <span class="identifier">C</span><span class="special">::</span><span class="identifier">const_iterator</span> <span class="identifier">iterator</span><span class="special">;</span></pre>
<p>
          Otherwise:
        </p>
<pre class="programlisting"><span class="keyword">typedef</span> <span class="keyword">typename</span> <span class="identifier">C</span><span class="special">::</span><span class="identifier">iterator</span> <span class="identifier">iterator</span><span class="special">;</span></pre>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="high_level_components.boost_python_iterator_hpp.class_template_iterators_static_"></a><a class="link" href="boost_python_iterator_hpp.html#high_level_components.boost_python_iterator_hpp.class_template_iterators_static_" title="Class template iterators static functions">Class
        template iterators static functions</a>
</h3></div></div></div>
<pre class="programlisting"><span class="keyword">static</span> <span class="identifier">iterator</span> <span class="identifier">begin</span><span class="special">(</span><span class="identifier">C</span><span class="special">&amp;);</span></pre>
<div class="variablelist">
<p class="title"><b></b></p>
<dl class="variablelist">
<dt><span class="term">Returns</span></dt>
<dd><p>
                <code class="computeroutput"><span class="identifier">x</span><span class="special">.</span><span class="identifier">begin</span><span class="special">()</span></code>
              </p></dd>
</dl>
</div>
<pre class="programlisting"><span class="keyword">static</span> <span class="identifier">iterator</span> <span class="identifier">end</span><span class="special">(</span><span class="identifier">C</span><span class="special">&amp;);</span></pre>
<div class="variablelist">
<p class="title"><b></b></p>
<dl class="variablelist">
<dt><span class="term">Returns</span></dt>
<dd><p>
                <code class="computeroutput"><span class="identifier">x</span><span class="special">.</span><span class="identifier">end</span><span class="special">()</span></code>
              </p></dd>
</dl>
</div>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="high_level_components.boost_python_iterator_hpp.functions"></a><a class="link" href="boost_python_iterator_hpp.html#high_level_components.boost_python_iterator_hpp.functions" title="Functions">Functions</a>
</h3></div></div></div>
<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">NextPolicies</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Target</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Accessor1</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Accessor2</span><span class="special">&gt;</span>
<span class="identifier">object</span> <span class="identifier">range</span><span class="special">(</span><span class="identifier">Accessor1</span> <span class="identifier">start</span><span class="special">,</span> <span class="identifier">Accessor2</span> <span class="identifier">finish</span><span class="special">);</span>

<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">NextPolicies</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Accessor1</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Accessor2</span><span class="special">&gt;</span>
<span class="identifier">object</span> <span class="identifier">range</span><span class="special">(</span><span class="identifier">Accessor1</span> <span class="identifier">start</span><span class="special">,</span> <span class="identifier">Accessor2</span> <span class="identifier">finish</span><span class="special">);</span>

<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">Accessor1</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Accessor2</span><span class="special">&gt;</span>
<span class="identifier">object</span> <span class="identifier">range</span><span class="special">(</span><span class="identifier">Accessor1</span> <span class="identifier">start</span><span class="special">,</span> <span class="identifier">Accessor2</span> <span class="identifier">finish</span><span class="special">);</span>
</pre>
<div class="variablelist">
<p class="title"><b></b></p>
<dl class="variablelist">
<dt><span class="term">Requires</span></dt>
<dd><p>
                NextPolicies is a default-constructible model of <a class="link" href="../concepts.html#concepts.callpolicies" title="CallPolicies">CallPolicies</a>.
              </p></dd>
<dt><span class="term">Effects</span></dt>
<dd>
<p>
                The first form creates a Python callable object which, when invoked,
                converts its argument to a Target object x, and creates a Python
                iterator which traverses <code class="computeroutput"><span class="special">[</span><span class="identifier">bind</span><span class="special">(</span><span class="identifier">start</span><span class="special">,</span><span class="identifier">_1</span><span class="special">)(</span><span class="identifier">x</span><span class="special">),</span> <span class="identifier">bind</span><span class="special">(</span><span class="identifier">finish</span><span class="special">,</span><span class="identifier">_1</span><span class="special">)(</span><span class="identifier">x</span><span class="special">))</span></code>,
                applying NextPolicies to the iterator's <code class="computeroutput"><span class="identifier">next</span><span class="special">()</span></code> function. The second form is identical
                to the first, except that Target is deduced from Accessor1 as follows:
              </p>
<div class="orderedlist"><ol class="orderedlist" type="1">
<li class="listitem">
                    If Accessor1 is a function type, Target is the type of its first
                    argument.
                  </li>
<li class="listitem">
                    If Accessor1 is a data member pointer of the form <code class="computeroutput"><span class="identifier">R</span> <span class="special">(</span><span class="identifier">T</span><span class="special">::*)</span></code>,
                    Target is identical to <code class="computeroutput"><span class="identifier">T</span></code>.
                  </li>
<li class="listitem">
                    If Accessor1 is a member function pointer of the form <code class="computeroutput"><span class="identifier">R</span> <span class="special">(</span><span class="identifier">T</span><span class="special">::*)(</span><span class="identifier">arguments</span><span class="special">...)</span>
                    <span class="identifier">cv</span><span class="special">-</span><span class="identifier">opt</span></code>, where cv-opt is an optional
                    cv-qualifier, Target is identical to <code class="computeroutput"><span class="identifier">T</span></code>.
                  </li>
</ol></div>
<p>
                The third form is identical to the second, except that NextPolicies
                is an unspecified model of <a class="link" href="../concepts.html#concepts.callpolicies" title="CallPolicies">CallPolicies</a>
                which always makes a copy of the result of deferencing the underlying
                C++ iterator
              </p>
</dd>
<dt><span class="term">Rationale</span></dt>
<dd><p>
                The use of <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">bind</span><span class="special">()</span></code> allows C++ iterators to be accessed
                through functions, member functions or data member pointers. Customization
                of NextPolicies (e.g. using <a class="link" href="../function_invocation_and_creation/models_of_callpolicies.html#function_invocation_and_creation.models_of_callpolicies.boost_python_return_internal_ref.class_template_return_internal_r" title="Class template return_internal_reference">return_internal_reference</a>)
                is useful when it is expensive to copy sequence elements of a wrapped
                class type. Customization of Target is useful when Accessor1 is a
                function object, or when a base class of the intended target type
                would otherwise be deduced.
              </p></dd>
</dl>
</div>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="high_level_components.boost_python_iterator_hpp.example"></a><a class="link" href="boost_python_iterator_hpp.html#high_level_components.boost_python_iterator_hpp.example" title="Example">Example</a>
</h3></div></div></div>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="identifier">module</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="keyword">class</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>

<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">vector</span><span class="special">&gt;</span>

<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">;</span>
<span class="identifier">BOOST_PYTHON_MODULE</span><span class="special">(</span><span class="identifier">demo</span><span class="special">)</span>
<span class="special">{</span>
    <span class="identifier">class_</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;</span> <span class="special">&gt;(</span><span class="string">"dvec"</span><span class="special">)</span>
        <span class="special">.</span><span class="identifier">def</span><span class="special">(</span><span class="string">"__iter__"</span><span class="special">,</span> <span class="identifier">iterator</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;</span> <span class="special">&gt;())</span>
        <span class="special">;</span>
<span class="special">}</span>
</pre>
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2002-2005, 2015 David Abrahams, Stefan Seefeld<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="boost_python_init_hpp.html"><img src="../../images/prev.png" alt="Prev"></a><a accesskey="u" href="../high_level_components.html"><img src="../../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="boost_python_module_hpp.html"><img src="../../images/next.png" alt="Next"></a>
</div>
</body>
</html>
